<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="60%">
  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="备份文件" align="center" prop="fileName" />
      <el-table-column label="文件大小" align="center" prop="size" />
      <el-table-column
        label="备份时间"
        align="center"
        prop="backupTime"
        width="180"
        :formatter="dateFormatter"
      />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="danger"
            @click="handleDownload(scope.row.id,scope.row.fileName)"
            v-hasPermi="['cms:db-backup:backup']"
          >
            下载
          </el-button>
          <el-button
            link
            type="danger"
            @click="openForm(scope.row.fileName, scope.row.id)"
            v-hasPermi="['cms:db-backup:restore']"
          >
            还原
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id,scope.row.fileName)"
            v-hasPermi="['cms:db-backup:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </ContentWrap>

    <template #footer>
      <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>

    <DbRestoreForm ref="formRef" @success="restoreFinish" />
  </Dialog>
</template>
<script lang="ts" setup>
import DbRestoreForm from './DbRestoreForm.vue'
const formRef = ref()
const openForm = (fileName:string,id: number) => {
  formRef.value.open(dbType.value,fileName,id)
}
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const dbType = ref('MySQL') // 数据库类型
/** 打开弹窗 */
const open = async (dbTypeVal:string,id: number) => {
  dialogVisible.value = true
  dialogTitle.value = "备份文件"
  dbType.value = dbTypeVal;
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      getList(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗
import { dateFormatter } from '@/utils/formatTime'
import * as DataBackupApi from '@/api/infra/dbbackup'
import download from '@/utils/download'

defineOptions({ name: 'DbFilesDialog' })

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据

/** 查询列表 */
const getList = async (id:number) => {
  loading.value = true
  try {
    list.value = await DataBackupApi.getDbBackupFiles(id)
  } finally {
    loading.value = false
  }
}

/** 恢复数据库 关闭后弹窗 */
const restoreFinish = async () => {
  dialogVisible.value = false
}

/** 下载按钮操作 */
const handleDownload = async (id: number,fileName:string) => {
  try {
    // 发起下载
    const res = await DataBackupApi.downloadDbBackupFile(id,fileName)
    download.zip(res, fileName)
    message.success(t('common.success'))
    // 刷新列表
    await getList(id)
  } catch {}
}
/** 删除按钮操作 */
const handleDelete = async (id: number,fileName:string) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await DataBackupApi.deleteDbBackupFile(id,fileName)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList(id)
  } catch {}
}
</script>
